﻿using System;
using EmbeddedSharp.Core;
using Zove.Runtime;

namespace Zove.Scripting.Modelling {
    /// <summary>
    /// Reset an instance or a factor to its default status.
    /// </summary>
    [Function("reset", "Reset an instance or a factor to its default status.",
        "circle c;\n\rc.r=1;\n\recho(c.area);\n\rreset(c);\n\recho(c.area);\n\r",
        "c.area!=3.1415926")]
    public class ResetNode : ZoveScriptingContextFuncNode {

        /// <summary>
        /// Default constructor
        /// </summary>
        public ResetNode() :
            base("reset") {
        }

        /// <summary>
        /// Evaluate the expression
        /// </summary>
        /// <returns></returns>
        public override object Evaluate() {
            if (Parameters.Count < 1) {
                throw new ArgumentException(
                    "usage: reset(factor);");
            }

            string factorName
                = Convert.ToString(Parameters[0].Value);
            RuntimeFactor factor
                = ZoveShellContext.GetFactor(factorName);
            if (factor != null) {
                factor.Reset();
                return true;
            }

            return false;
        }
    }
}
